package com.flyqiu.flow.core.deploy;

import com.alibaba.fastjson2.JSONObject;
import com.flyqiu.flow.api.deploy.IFlowDeploy;
import com.flyqiu.flow.api.deploy.FlyQiuFlowDeployManage;
import com.flyqiu.flow.common.enums.ProcDeployType;
import com.flyqiu.common.tools.StringTools;
import com.flyqiu.flow.model.entity.ProcDeploySource;

import java.util.HashMap;
import java.util.Map;

public class DefaultFlyQiuFlowDeployManage implements FlyQiuFlowDeployManage {
    private String defProcExecuteType = ProcDeployType.FULLY_AUTOMATIC.getValue();

    private final Map<String, IFlowDeploy> deployMap = new HashMap<>();

    /**
     * 注册流程发布插件
     *
     * @param deploy
     */
    public void registerProcDeploy(IFlowDeploy deploy) {
        if (deploy == null) {
            throw new RuntimeException("deploy  Cannot be null or empty");
        }
        if (StringTools.isEmpty(deploy.procDeployKey())) {
            throw new RuntimeException("procDeployKey  Cannot be null or empty");
        }

        deployMap.put(deploy.procDeployKey(), deploy);
    }

    /**
     * 获取默认执行类型key
     *
     * @return
     */
    public String getDefProcExecuteTypeKey() {
        return defProcExecuteType;
    }

    @Override
    public void setDefProcExecuteTypeKey(String key) {
        if (!deployMap.containsKey(key)) {
            throw new RuntimeException("cannot find deploy by key:" + key);
        }
        this.defProcExecuteType = key;

    }


    /**
     * 获取流程发布插件
     *
     * @param key
     * @return
     */
    public IFlowDeploy getDeploy(String key) {
        if (!deployMap.containsKey(key)) {
            throw new RuntimeException("cannot find deploy by key:" + key);
        }
        return deployMap.get(key);
    }


    @Override
    public ProcDeploySource deploy(JSONObject modelSource, String deployType, String procDefKey, String procName, String category) {
        IFlowDeploy deploy = getDeploy(deployType);
        return deploy.deploy(modelSource, procDefKey, procName, category);
    }


}
